Security News
Supply Chain Attack Detected in Solana's web3.js Library
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
appcd-dispatcher
Advanced tools
Provides an HTTP-like API for registering and invoking service endpoints.
Visit https://github.com/appcelerator/appc-daemon for more information.
npm i appcd-dispatcher
import Dispatcher from 'appcd-dispatcher';
const dispatcher = new Dispatcher();
dispatcher.register('/foo/:id?', ctx => {
if (ctx.request.params.id) {
return {
message: `hello ${ctx.request.params.id}!`
};
}
return {
message: 'hello guest!'
};
});
try {
// 200
let ctx = await dispatcher.call('/foo');
console.log('Response:', ctx.response);
// 200 with parameter
ctx = await dispatcher.call('/foo/123');
console.log('Response:', ctx.response);
// 404
await dispatcher.call('/bar');
} catch (err) {
console.error(err);
}
Dispatcher
has a static register()
, unregister()
, and call()
methods.
Dispatcher.register('/foo/:id?', ctx => {
const { id } = ctx.request.params;
ctx.response = {
message: `hello ${id || 'guest'}!`
};
});
Dispatcher.register('/foo', ({ response }) => {
setInterval(() => {
response.write(new Date().toString());
}, 1000);
});
Response statuses are based on HTTP status codes. Successful calls return a 200
status, bad
requests return a 400
status, route not found returns a 404
status, and errors return a 500
status.
Route handlers can throw any Error
and the dispatcher will return it as a 500
.
appcd-response provides Response
and AppcdError
classes to assist with return messages with specific text and statuses.
ServiceDispatcher
A ServiceDispatcher
is an abstract base class for implementing services that support calls and
subscriptions. You must not directly instantiate a ServiceDispatcher
, but rather define your own
class that extends it.
If your service just needs to serve a simple object or array dataset, such as a gawked object, then
you should use the DataServiceDispatcher
. ServiceDispatcher
is
intended for intances where you need strict control.
Examples of a ServiceDispatcher
being used are the ConfigService
and
FSWatchManager
.
import { ServiceDispatcher } from 'appcd-dispatcher';
export default class MyService extends ServiceDispatcher {
onCall(ctx) {
return 'Hello from my service';
}
}
The following are all of the methods that your ServiceDispatcher
derived class can implement:
onCall(ctx)
Handles a request with a single response. Note that the response can be a stream, but not efficient if there are multiple incoming requests.
Param | Type | Description |
---|---|---|
ctx | DispatcherContext (docs) | A dispatcher context containing the original request and a response. |
Returns anything. When result is a DispatcherContext
, it is returned to the Dispatcher
. When
result is not undefined
, then it is stored in the response of the DispatcherContext
(ctx.response
) and returned to the dispatcher. onCall()
may also return a Promise
which
resolves a response as previously described.
This method is optional.
getTopic(ctx)
Returns a topic based on the request context.
Param | Type | Description |
---|---|---|
ctx | DispatcherContext (docs) | A dispatcher context containing the original request and a response. |
Returns a String
.
This method is optional. By default, the ServiceDispatcher
will use the ctx.realPath
.
initSubscription({ ctx, publish(), sid, topic })
Called before the first subscription for the given topic is requested. If all subscriptions have been unsubscribed, then the next subscription event would invoke this callback.
Param | Type | Description |
---|---|---|
ctx | DispatcherContext (docs) | A dispatcher context containing the original request and a response. |
publish() | Function | A function that should be called when your service wants to publish an event to all topic subscribers. |
sid | String | The subscription id. Useful for unsubscribing. |
topic | String | The subscription topic derived from getTopic() or ctx.realPath . |
This method returns undefined
and is optional unless you are using onSubscribe()
.
onSubscribe({ ctx, publish(), sid, topic })
Called when a new subscription is requested. Requires the initSubscription()
method to be defined.
Param | Type | Description |
---|---|---|
ctx | DispatcherContext (docs) | A dispatcher context containing the original request and a response. |
publish() | Function | A function that should be called when your service wants to publish an event to all topic subscribers. |
sid | String | The subscription id. Useful for unsubscribing. |
topic | String | The subscription topic derived from getTopic() or ctx.realPath . |
This method returns undefined
and is optional.
onUnsubscribe({ ctx, publish(), sid, topic })
Called when a subscription has been unsubscribed. Requires the destroySubscription()
method to be defined.
Param | Type | Description |
---|---|---|
ctx | DispatcherContext (docs) | A dispatcher context containing the original request and a response. |
sid | String | The subscription id. Useful for unsubscribing. |
topic | String | The subscription topic derived from getTopic() or ctx.realPath . |
This method returns undefined
and is optional.
destroySubscription()
Called after the last subscription for the given topic has been unsubscribed.
Param | Type | Description |
---|---|---|
ctx | DispatcherContext (docs) | A dispatcher context containing the original request and a response. |
publish() | Function | A function that should be called when your service wants to publish an event to all topic subscribers. |
sid | String | The subscription id. Useful for unsubscribing. |
topic | String | The subscription topic derived from getTopic() or ctx.realPath . |
Note that publish()
is not intended to be invoked, but rather used to remove any event listeners.
This method returns undefined
and is optional unless you are using onUnsubscribe()
.
DataServiceDispatcher
A DataServiceDispatcher
is a ServiceDispatcher
implementation for wiring up a object or array
dataset to a data-only service. It gives you automatic data responses with filtering and
subscriptions.
DataServiceDispatcher
has an property called data
. It must be a gawked object. You can
either pass an object into the constructor or override this.data
with your own gawked object.
If you pass in an object, the DataServiceDispatcher
will gawk it for you, then directly update
properties of data
.
import { DataServiceDispatcher } from 'appcd-dispatcher';
export default class MyDataService extends DataServiceDispatcher {
constructor() {
super({
ts: Date.now()
});
setInterval(() => {
this.data.ts = Date.now();
}, 1000);
}
}
Examples of a DataServiceDispatcher
being used are the StatusMonitor
and
PluginManagerStatus
.
This project is open source under the Apache Public License v2 and is developed by
Axway, Inc and the community. Please read the LICENSE
file included
in this distribution for more information.
FAQs
Provides HTTP-like API for exposing services.
The npm package appcd-dispatcher receives a total of 3 weekly downloads. As such, appcd-dispatcher popularity was classified as not popular.
We found that appcd-dispatcher demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.